package com.mall.manager.sellstatic.service;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class SellStaticService {

	private Logger logger = LoggerFactory.getLogger(SellStaticService.class);
	
	 @Resource(name="jdbcTemplate")
	 private JdbcTemplate jdbcTemplate;
	
	 /**
	  * 查询全国园所
	  * @param request
	  * @return
	  */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List findBabyTop(HttpServletRequest request){
	    try{
	    	//如果是第一次加载地图，按省级（包括直辖市）查询园所数量
	    	String sqlp = "select * from db_archive.province";
	    	List list = jdbcTemplate.queryForList(sqlp);
	    	StringBuffer codes = new StringBuffer("'");
	    	JSONArray ar = JSONArray.fromObject(list);
	    	for(int i=0;i<ar.size();i++){
	    		Map<String,Object> map = JSONObject.fromObject(ar.get(i));
	    		codes.append(MapUtils.getString(map, "code")).append("','");
	    	}    		
    		StringBuffer sbf = new StringBuffer("select * from db_processing.t_sell_statistics as s where 1=1");
    		String cd = codes.toString().substring(0, codes.toString().length()-2);
    		sbf.append(" and s.area_id in(").append(cd).append(")");
    		return this.jdbcTemplate.queryForList(sbf.toString());
	    }catch(Exception e){
	    	this.logger.info("查询全园所数量出现异常："+e.getMessage());
	    }
		return null;
	}
}
